Borrar filtros
Borrar filtros

Editing a function and then using fzero

2 visualizaciones (últimos 30 días)
Tevel Hedi
Tevel Hedi el 8 de Abr. de 2022
Comentada: Tevel Hedi el 8 de Abr. de 2022
I am trying to write a code that uses fzero on a function.
As I see it the only one way to edit and ran scripts on a function is by declaring it as syms type.
But syms does not work with fzero.
How should I approach it?
* I am trying to solve a nonlinear equation with two variables. So I have made two syms variables and wrote two function with those syms variables. So I get two syms functions. Then I use subs on two of the functions in order to have a one variable equation using those two functions (sub syms type to a scalar). I a adding the code.
clear
clc
%variables
u0 = 4*pi*10e-7;
Ms = 0.8;
gamma = 1;
Ku=1;
D = 0;
A = 1;
sinfei = sin(-pi/2);
delta=1;
syms d W
for m = 1:2
surfaceV(m) = (sin((pi/gamma)*W*m)/sinh((pi^2/gamma)*delta*m))^2*((1-exp(-(2*pi/gamma)*m*d))/m);
volumeV(m) = (sin((pi/gamma)*W*m)/cosh((pi^2/gamma)*delta*m))^2*((exp(-(2*pi/gamma)*m*d)+((2*pi/gamma)*m*d)-1)/m);
end
surfaceEnergy = (1/2)*u0*Ms^2*(1-2*W/gamma)^2+((2*pi*u0*Ms^2*delta^2)/(gamma*d))*sum(surfaceV);
volumeEnergy = ((2*pi*u0*Ms^2*delta^2*sinfei^2)/(gamma*d))*sum(surfaceV);
dW_surfaceEnergy = diff(surfaceEnergy,W);
dW_volumeEnergy = diff(volumeEnergy,W);
non_volumeORsurface_energy=-((2*delta*Ku+2*A/delta+pi*D*sinfei)/W^2); %Non volume nor surface energies for total energy solution
d_vector = double(linspace(100,200,3));
W_d=double(zeros(1,length(d_vector),'uint64'));
for m = 1:length(d_vector)
W_d(m)=double(fzero(subs(symfun(dW_surfaceEnergy+dW_volumeEnergy+non_volumeORsurface_energy,W),d,d_vector(m)),50));
end
  2 comentarios
Torsten
Torsten el 8 de Abr. de 2022
As I see it the only one to edit and ran scripts on a function is by declaring it as sys type.
I don't know what you mean.
Jan
Jan el 8 de Abr. de 2022
Please tell us more details.

Iniciar sesión para comentar.

Respuesta aceptada

Torsten
Torsten el 8 de Abr. de 2022
Editada: Torsten el 8 de Abr. de 2022
%variables
u0 = 4*pi*10e-7;
Ms = 0.8;
gamma = 1;
Ku=1;
D = 0;
A = 1;
sinfei = sin(-pi/2);
delta=1;
syms d W
for m = 1:2
surfaceV(m) = (sin((pi/gamma)*W*m)/sinh((pi^2/gamma)*delta*m))^2*((1-exp(-(2*pi/gamma)*m*d))/m);
volumeV(m) = (sin((pi/gamma)*W*m)/cosh((pi^2/gamma)*delta*m))^2*((exp(-(2*pi/gamma)*m*d)+((2*pi/gamma)*m*d)-1)/m);
end
surfaceEnergy = (1/2)*u0*Ms^2*(1-2*W/gamma)^2+((2*pi*u0*Ms^2*delta^2)/(gamma*d))*sum(surfaceV);
volumeEnergy = ((2*pi*u0*Ms^2*delta^2*sinfei^2)/(gamma*d))*sum(surfaceV);
dW_surfaceEnergy = diff(surfaceEnergy,W);
dW_volumeEnergy = diff(volumeEnergy,W);
non_volumeORsurface_energy=-((2*delta*Ku+2*A/delta+pi*D*sinfei)/W^2); %Non volume nor surface energies for total energy solution
expression = matlabFunction(dW_surfaceEnergy+dW_volumeEnergy+non_volumeORsurface_energy)
d_vector = linspace(100,200,30);
W_d=zeros(1,length(d_vector));
W0 = 1;
format long
for i=1:numel(d_vector)
d = d_vector(i);
W_d(i) = fzero(@(W)expression(W,d),W0);
error = expression(W_d(i),d)
W0 = W_d(i);
end
The solutions don't differ much - but they are solutions as can be seen by error.

Más respuestas (0)

Categorías

Más información sobre General Physics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by