Conversion of Mathematica code to matlab

I have the following code in mathematical and need to convert it into matlab but cant for the life of me understand ow to do it, though i think i need to use the 'fzero'; command. Please could someone help me or give me a hint?
function 'f' has already been defined but i need to find its roots for where variable a is from 0-0.6 etc
list1=For[a=-0.01, a<0.6, a+=0.01; sol=Findroot[f,{u, 1.00,1.10}];
v1[i] = sol[[1,2]]; i++]

2 commentaires

Walter Roberson
Walter Roberson le 4 Mar 2013
Is f defined in terms of "a" ?
Mikhos
Mikhos le 4 Mar 2013
yeh it is

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 4 Mar 2013
vl = arrayfun(@(a) fzero( @(x) f(x,a), [1 1.1] ), -0.01 : 0.01 : 0.6-eps);
the "-eps" is to recreate the "<" part of "<0.6". Using 0.5 instead might work.

12 commentaires

cheers for your help so far. the error below occurred.
Error using fzero (line 233)
FZERO cannot continue because user supplied function_handle ==> @(x)f(x,a) failed with the error below.
Too many input arguments.
Error in @(a)fzero(@(x)f(x,a),[1,1.1])
My plan was to solve f and use it to plot a with respect to v with constant w using the code below. I wonder if there is something else im missing.
w =0.001;
syms a v
g = @(a) (2048*((1+v).^4) + 128*((1+v).^2).*a.^6 + a.^12 + 16*(1+v).*sqrt(16384*((1+v).^6) + 2048*((1+v).^4).*a.^6 + 80*((1+v).^2).*a.^12 + a.^18)).^(1/3);
r =@(a) (1/(16*(1+v))).*( a.^4 + (a.^8)./g(a) + g(a) );
alpha= @(a) (a./r(a));
A= @(a) 2*pi*(r(a).^2)*(1+sqrt(1-alpha(a).^2));
epsilon = @(a) 1/2*(A(a)/(4*pi-pi*a.^2)-1);
f= @(a) (1-sqrt(1-alpha(a).^2)).*eps(a) + (eps(a).^2) - w == 0;
vl = arrayfun(@(a) fzero( @(x) f(x,a), [1 1.1] ), -0.01 : 0.01 : 0.6-eps);
Walter Roberson
Walter Roberson le 11 Mar 2013
Your original question implies that f is a function of both "a" and "u", but you have no "u" in your f. It appears though that you have an unresolved symbol "v" hovering around.
What purpose is your "epsilon" function? You do not use it. Perhaps in your definition of f, you meant to use epsilon(a) instead of eps(a) ?
Mikhos
Mikhos le 12 Mar 2013
quite right, I do apologise for my sloppy coding. Firstly it I have been using v instead of u. and secondly yes I originally used eps(a) but changed it into espilon(a) as i was worried it might cause confusion when I found that "eps" can also be a command (but forgot to change the other formulae).
I have changed all 'eps(a)' values into espilon(a) however the same error still appears.
Walter Roberson
Walter Roberson le 12 Mar 2013
Please show your current coding
w =0.001;
syms a v
g = @(a) (2048*((1+v).^4) + 128*((1+v).^2).*a.^6 + a.^12 + 16*(1+v).*sqrt(16384*((1+v).^6) + 2048*((1+v).^4).*a.^6 + 80*((1+v).^2).*a.^12 + a.^18)).^(1/3);
r =@(a) (1/(16*(1+v))).*( a.^4 + (a.^8)./g(a) + g(a) );
alpha= @(a) (a./r(a));
A= @(a) 2*pi*(r(a).^2)*(1+sqrt(1-alpha(a).^2));
epsilon = @(a) 1/2*(A(a)/(4*pi-pi*a.^2)-1);
f= @(a) (1-sqrt(1-alpha(a).^2)).*epsilon(a) + (epsilon(a).^2) - w == 0;
vl = arrayfun(@(a) fzero( @(x) f(x,a), [1 1.1] ), -0.01 : 0.01 : 0.6-eps);
w =0.001;
g = @(a, v) (2048*((1+v).^4) + 128*((1+v).^2).*a.^6 + a.^12 + 16*(1+v).*sqrt(16384*((1+v).^6) + 2048*((1+v).^4).*a.^6 + 80*((1+v).^2).*a.^12 + a.^18)).^(1/3);
r =@(a, v) (1/(16*(1+v))).*( a.^4 + (a.^8)./g(a, v) + g(a, v) );
alpha= @(a, v) (a./r(a, v));
A= @(a, v) 2*pi*(r(a, v).^2)*(1+sqrt(1-alpha(a, v).^2));
epsilon = @(a, v) 1/2*(A(a, v)/(4*pi-pi*a.^2)-1);
f= @(a, v) (1-sqrt(1-alpha(a, v).^2)).*epsilon(a, v) + (epsilon(a, v).^2) - w == 0;
vl = arrayfun(@(a) fzero( @(v) f(a,v), [1 1.1] ), -0.01 : 0.01 : 0.6-eps);
I think.
Mikhos
Mikhos le 12 Mar 2013
sorry to be such a pain and thank you for all your help so far. But vl comes out as a 1x62 matrix of 1s
Walter Roberson
Walter Roberson le 12 Mar 2013
Yup. The first part of your expression for f is producing a non-zero value, and the second part compares that non-zero value to 0, producing a logical false, and logic false has numeric value 0, so when fzero() is looking for a location that makes the function 0, it finds it first time around. Not sure why you would want the "== 0" in there...
If you remove the "== 0" and investigate a whole bunch, you will find that your expression has no real roots for that range of "a" within the interval [1 1.1]
Mikhos
Mikhos le 12 Mar 2013
ok i think i get that, at least in part. So essentially what i need to do is plot a graph of v between 0-0.1 with respect to a for a certain value of w. With the following given equations
g = @(a) (2048*((1+v).^4) + 128*((1+v).^2).*a.^6 + a.^12 + 16*(1+v).*sqrt(16384*((1+v).^6) + 2048*((1+v).^4).*a.^6 + 80*((1+v).^2).*a.^12 + a.^18)).^(1/3);
r =@(a) (1/(16*(1+v))).*( a.^4 + (a.^8)./g(a) + g(a) );
alpha= @(a) (a./r(a));
A= @(a) 2*pi*(r(a).^2)*(1+sqrt(1-alpha(a).^2));
eps = @(a) 1/2*(A(a)/(4*pi-pi*a.^2)-1);
w = @(a) (1-sqrt(1-alpha(a).^2)).*eps(a) + (eps(a).^2)
The problem is i have been trying different things for literally weeks and get really confused quite where to go from here. I know I need to find roots but I dont quite get what of or how to use the command.
Walter Roberson
Walter Roberson le 12 Mar 2013
Why did you remove the ",v" in the argument lists?
Mikhos
Mikhos le 13 Mar 2013
because i copied them from a previous file. In that file i was plotting w with respect to a for five specific values of v so I treated it like a constant

Connectez-vous pour commenter.

ABHIJAY PANDEY
ABHIJAY PANDEY le 6 Sep 2016

0 votes

f[x_]=0.09*sin[x]+0.085*sin[x-1] plot[f[x],{x,-2,2}] can anybody please tell me the matlab code for this mathematica code.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by