I need help with Matlab
显示 更早的评论


function [y] = hwk1prob1(x)
hwk1prob1= @(x)[tanh(2*x(1)-3*x(2)^2)+1;
exp(.3*x(1)+.4*x(2))-4.7];
y = fsolve( myhwk1prob1,x)
end
This is the code I have written for solving this problem yet I keep runnig into errors such as not enough input arguments, undefined function. I am really new to matlab so any help would be greatly appreciated.
回答(2 个)
Star Strider
2019-2-10
The problem is that you named your parent function and your anonymous function the same. That’s going to cause problems.
Try this slightly edited version:
function [y] = hwk1prob1(x)
hwk1prob1fcn = @(x)[tanh(2*x(1)-3*x(2)^2)+1;
exp(.3*x(1)+.4*x(2))-4.7];
y = fsolve(hwk1prob1fcn,x);
end
x = [42; 42];
y = hwk1prob1(x) % Function Call
5 个评论
Patrick Potts
2019-2-10
编辑:Patrick Potts
2019-2-10
Star Strider
2019-2-10
The code I posted ran for me without error. I would not have posted it otherwise.
It produced:
y =
10.5195
-4.0207
Walter Roberson
2019-2-10
The two lines at the bottom, x = [42; 42]; y = hwk1prob1(x); should go outside of the hwk1prob1.m file . They are showing how to execute the code.
However... your assignment is to create a hwk1prob1.m that does not expect any input, and which determines its own x value to pass as the second parameter to fsolve()
Patrick Potts
2019-2-10
Star Strider
2019-2-11
If my Answer helped you solve your problem, pleae Accept it!
Walter Roberson
2019-2-10
0 个投票
You are asked to write a program hwk1prob1.m . It is not asked to be a function . No input will be passed to it.
Your code needs to try several different x values to find one that works.
Numerically, your function has solutions because of round-off error. Mathematically at least one of the two inputs needs to be infinite for there to be a solution. This is partly because the code shown for fun1 does not implement the equation in the description. Look at where the 1 is relative to the tanh() call: in the equation it is inside the tanh() but in the code it is outside the tanh() . This makes a large difference in the meaning of the equation.
2 个评论
Patrick Potts
2019-2-10
Walter Roberson
2019-2-10
x0 = SomethingThatYouWillHaveToFigureOut
fsolve(@fun1, x0)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!